layui.define(['table', 'form', 'layer', 'setter', 'view', 'admin'], function (exports) {
    var $ = layui.$,
        admin = layui.admin,
        view = layui.view,
        table = layui.table,
        form = layui.form,
        layer = layui.layer,
        router = layui.router(),
        setter = layui.setter;

    table.render({
        elem: '#datatable',
        page: true,
        url: setter.url + '/serialName/getSerialNumber',
        where: {
            userName: layui.data('userInfo').data.exname,
            token: layui.data('userInfo').data.token
        },
        request: {
            pageName: 'pageindex',
            limitName: 'pagesize'
        },
        method: 'post',
        response: {
            statusName: 'code',
            statusCode: 200,
            msgName: 'message',
            countName: 'count',
            dataName: 'rows'
        },
        cols: [
            [{
                    field: 'serialNumber',
                    title: '序列号'
                },
                {
                    field: 'description',
                    title: '描述'
                },
                {
                    field: 'createTime',
                    title: '创建时间'
                },
                {
                    title: '操作',
                    toolbar: '#toolbar'
                }
            ]
        ]


    })

    //   监听工具条
    table.on('tool(datatable)', function (obj) {
        var datas = obj.data
        var description = obj.data.description
        var serialNumber = obj.data.serialNumber
        console.log(datas);
        if (obj.event === 'copy') {
            var text = datas.serialNumber
            let value = $("<textarea>" + text + "</textarea>")
            $("body").append(value);
            value[0].select(); // 选择对象
            document.execCommand("Copy");
            layer.msg('复制成功');
            value.remove();
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '修改备注',
                area: ['400px', '400px'],
                id: 'LAY-dataSwitch-edit',
                success: function (layero, index) {
                    view(this.id).render('gateway/gatewayEdit', {
                            description
                        })
                        .done(function () {
                            form.on('submit(gatewayEdit-edit-submit)', function (data) {
                                var field = data.field;
                                console.log(field);
                                admin.req({
                                    url: setter.url + '/gateway/updateGatewayDescription',
                                    data: {
                                        ...field,
                                        serialNumber
                                    },
                                    type: 'post',
                                    done: function (res) {
                                        layer.msg(res.message);
                                        parent.location.reload();
                                        layer.close(index)
                                    }
                                })
                            })
                        })
                }
            })
        } else if (obj.event === 'del') {
            layer.confirm('确定要删除这条吗？', {
                btn: ['删除', '取消']
            }, function (index) {
                admin.req({
                    url: setter.url + '/serialName/deleteSerialNumber',
                    data: {
                        mid: datas.id
                    },
                    method: 'post',
                    done: function (res) {
                        if (res.code === '200') {
                            layer.msg(res.message)
                            layui.table.reload('datatable')
                        }
                    }
                })
                layer.close(index)
            })
        }
    })

    exports('dataSwitch', {})

});